/******************************************************************************************************
Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

function predictedMoleculeTest()
{
	'use strict';
	var mol = new Molecule(Application.molecule.activeMolecule()),
		predictionResult;

	if (Application.NMRPredictor === undefined)
		throw "Predictor Plugin not loaded";

	if (!mol.isValid())
		throw "Invalid Molecule";

	predictionResult = mol.nmrPrediction("1H", true);
	print(JSON.stringify(predictionResult));
}

function setMoleculePrediction()
{
	'use strict';
	var predStr = '',
		predObj = JSON.parse('[{"atom":{"index":1,"h":"a"},"shift":{"value":1.9000000953674316,"error":5}},{"atom":{"index":2,"h":"a"},"shift":{"value":5.4800004959106445,"error":0.2},"js":[{"atom":{"index":16},"j":{"value":9.300000190734863,"error":-1}}]},{"atom":{"index":6,"h":"a"},"shift":{"value":1.0000001192092896,"error":0.35},"js":[{"atom":{"index":10,"h":"a"},"j":{"value":1.5,"error":-1}},{"atom":{"index":11,"h":"a"},"j":{"value":0.5,"error":0.5}},{"atom":{"index":15,"h":"a"},"j":{"value":0.5,"error":0.5}}]},{"atom":{"index":9,"h":"a"},"shift":{"value":3.81000018119812,"error":0.2},"js":[{"atom":{"index":9,"h":"b"},"j":{"value":-11.5,"error":-1}},{"atom":{"index":9,"h":"c"},"j":{"value":-11.5,"error":-1}}]},{"atom":{"index":9,"h":"b"},"shift":{"value":3.81000018119812,"error":0.2},"js":[{"atom":{"index":9,"h":"a"},"j":{"value":-11.5,"error":-1}},{"atom":{"index":9,"h":"c"},"j":{"value":-11.5,"error":-1}}]},{"atom":{"index":9,"h":"c"},"shift":{"value":3.81000018119812,"error":0.2},"js":[{"atom":{"index":9,"h":"a"},"j":{"value":-11.5,"error":-1}},{"atom":{"index":9,"h":"b"},"j":{"value":-11.5,"error":-1}}]},{"atom":{"index":10,"h":"a"},"shift":{"value":7.3000006675720215,"error":0.35},"js":[{"atom":{"index":6,"h":"a"},"j":{"value":1.5,"error":-1}},{"atom":{"index":11,"h":"a"},"j":{"value":7.5,"error":-1}}]},{"atom":{"index":11,"h":"a"},"shift":{"value":7.950000286102295,"error":0.35},"js":[{"atom":{"index":10,"h":"a"},"j":{"value":7.5,"error":-1}},{"atom":{"index":6,"h":"a"},"j":{"value":0.5,"error":0.5}}]},{"atom":{"index":14,"h":"a"},"shift":{"value":8.620000839233398,"error":0.2},"js":[{"atom":{"index":15,"h":"a"},"j":{"value":7.5,"error":-1}}]},{"atom":{"index":15,"h":"a"},"shift":{"value":7.210000514984131,"error":0.35},"js":[{"atom":{"index":14,"h":"a"},"j":{"value":7.5,"error":-1}},{"atom":{"index":6,"h":"a"},"j":{"value":0.5,"error":0.5}}]},{"atom":{"index":16},"shift":{"value":3.120000123977661,"error":0.2},"js":[{"atom":{"index":2,"h":"a"},"j":{"value":9.300000190734863,"error":-1}},{"atom":{"index":21,"h":"a"},"j":{"value":8.100000381469727,"error":-1}},{"atom":{"index":21,"h":"b"},"j":{"value":8.100000381469727,"error":-1}}]},{"atom":{"index":18,"h":"a"},"shift":{"value":2.630000352859497,"error":0.2},"js":[{"atom":{"index":18,"h":"b"},"j":{"value":-12.5,"error":-1}},{"atom":{"index":19,"h":"a"},"j":{"value":7.900000095367432,"error":-1}}]},{"atom":{"index":18,"h":"b"},"shift":{"value":3.0500001907348633,"error":0.2},"js":[{"atom":{"index":18,"h":"a"},"j":{"value":-12.5,"error":-1}},{"atom":{"index":19,"h":"a"},"j":{"value":7.900000095367432,"error":-1}}]},{"atom":{"index":19,"h":"a"},"shift":{"value":2.2400002479553223,"error":0.35},"js":[{"atom":{"index":18,"h":"a"},"j":{"value":7.900000095367432,"error":-1}},{"atom":{"index":18,"h":"b"},"j":{"value":7.900000095367432,"error":-1}},{"atom":{"index":20,"h":"a"},"j":{"value":1.7999999523162842,"error":-1}},{"atom":{"index":24,"h":"a"},"j":{"value":6.199999809265137,"error":-1}},{"atom":{"index":25,"h":"a"},"j":{"value":0.4000000059604645,"error":-1}},{"atom":{"index":25,"h":"b"},"j":{"value":0.4000000059604645,"error":-1}}]},{"atom":{"index":20,"h":"a"},"shift":{"value":1.78000009059906,"error":0.2},"js":[{"atom":{"index":19,"h":"a"},"j":{"value":1.7999999523162842,"error":-1}},{"atom":{"index":21,"h":"a"},"j":{"value":3.200000047683716,"error":-1}},{"atom":{"index":21,"h":"b"},"j":{"value":3.200000047683716,"error":-1}},{"atom":{"index":22,"h":"a"},"j":{"value":3.0999999046325684,"error":-1}},{"atom":{"index":22,"h":"b"},"j":{"value":3.0999999046325684,"error":-1}}]},{"atom":{"index":21,"h":"a"},"shift":{"value":1.5500000715255737,"error":0.2},"js":[{"atom":{"index":16},"j":{"value":8.100000381469727,"error":-1}},{"atom":{"index":20,"h":"a"},"j":{"value":3.200000047683716,"error":-1}},{"atom":{"index":21,"h":"b"},"j":{"value":-13.199999809265137,"error":-1}}]},{"atom":{"index":21,"h":"b"},"shift":{"value":1.6900001764297485,"error":0.2},"js":[{"atom":{"index":16},"j":{"value":8.100000381469727,"error":-1}},{"atom":{"index":20,"h":"a"},"j":{"value":3.200000047683716,"error":-1}},{"atom":{"index":21,"h":"a"},"j":{"value":-13.199999809265137,"error":-1}}]},{"atom":{"index":22,"h":"a"},"shift":{"value":1.470000147819519,"error":0.2},"js":[{"atom":{"index":20,"h":"a"},"j":{"value":3.0999999046325684,"error":-1}},{"atom":{"index":22,"h":"b"},"j":{"value":-13.199999809265137,"error":-1}},{"atom":{"index":23,"h":"a"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":23,"h":"b"},"j":{"value":7.199999809265137,"error":-1}}]},{"atom":{"index":22,"h":"b"},"shift":{"value":1.6900001764297485,"error":0.2},"js":[{"atom":{"index":20,"h":"a"},"j":{"value":3.0999999046325684,"error":-1}},{"atom":{"index":22,"h":"a"},"j":{"value":-13.199999809265137,"error":-1}},{"atom":{"index":23,"h":"a"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":23,"h":"b"},"j":{"value":7.199999809265137,"error":-1}}]},{"atom":{"index":23,"h":"a"},"shift":{"value":2.630000352859497,"error":0.2},"js":[{"atom":{"index":22,"h":"a"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":22,"h":"b"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":23,"h":"b"},"j":{"value":-12.5,"error":-1}}]},{"atom":{"index":23,"h":"b"},"shift":{"value":3.380000352859497,"error":0.2},"js":[{"atom":{"index":22,"h":"a"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":22,"h":"b"},"j":{"value":7.199999809265137,"error":-1}},{"atom":{"index":23,"h":"a"},"j":{"value":-12.5,"error":-1}}]},{"atom":{"index":24,"h":"a"},"shift":{"value":5.7300004959106445,"error":0.2},"js":[{"atom":{"index":19,"h":"a"},"j":{"value":6.199999809265137,"error":-1}},{"atom":{"index":25,"h":"a"},"j":{"value":18.399999618530273,"error":-1}},{"atom":{"index":25,"h":"b"},"j":{"value":10.899999618530273,"error":-1}}]},{"atom":{"index":25,"h":"a"},"shift":{"value":5.006584644317627,"error":0.2},"js":[{"atom":{"index":19,"h":"a"},"j":{"value":0.4000000059604645,"error":-1}},{"atom":{"index":24,"h":"a"},"j":{"value":18.399999618530273,"error":-1}},{"atom":{"index":25,"h":"b"},"j":{"value":2,"error":-1}}]},{"atom":{"index":25,"h":"b"},"shift":{"value":5.006584644317627,"error":0.2},"js":[{"atom":{"index":19,"h":"a"},"j":{"value":0.4000000059604645,"error":-1}},{"atom":{"index":24,"h":"a"},"j":{"value":10.899999618530273,"error":-1}},{"atom":{"index":25,"h":"a"},"j":{"value":2,"error":-1}}]}]'),
		mol,
		nucleus = '1H',
		simParams = {nucleus: "1H", fromPPM: -2.0, toPPM: 10.0, size: 32768, frequency: 500.13, lineWidth: 0.75};

	mol = new Molecule(Application.molecule.activeMolecule());
	if (!mol.isValid())
		throw "Invalid Molecule";

	mol.setNMRPrediction("1H", predObj);
	Application.NMRPredictor.createPredictedSpectrum(mol, simParams);
}

function setMoleculePredictionFromFile()
{
	'use strict';
	var predStr = '',
		predObj,
		mol,
		nucleus = '1H',
		filename = FileDialog.getOpenFileName("*.json", "Select a prediction file", Dir.home()),
		file = new File(filename),
		simParams = {nucleus: "1H", fromPPM: -2.0, toPPM: 10.0, size: 32768, frequency: 500.13, lineWidth: 0.75};
	if (filename.length === 0)
		return;
	if(Application.NMRPredictor === undefined)
		throw "Predictor Plugin not loaded";

	mol = new Molecule(Application.molecule.activeMolecule());
	if(mol.isValid() === false)
		throw "Invalid Molecule";

	if (file.open(File.ReadOnly)) {
		predStr = file.readAll();
		predObj = JSON.parse(predStr);
		Application.NMRPredictor.setPredictedValues(mol,"1H", predObj, simParams);
	}
}

function deprecatedPredictedMoleculeTest()
{
        if(Application.NMRPredictor == undefined)
                throw "Predictor Plugin not loaded";

        var mol = new Molecule(Application.molecule.activeMolecule());
        if(mol.isValid()==false)
                throw "Invalid Molecule";

        var predictionResult = Application.NMRPredictor.predictionData(mol,"1H");
        if(predictionResult == undefined)
                throw "Molecule without prediction data";
        for(var i = 0; i<predictionResult.length; i++)
        {
                print(predictionResult[i].atom+predictionResult[i].H+": "+predictionResult[i].shift);
                for(var j=0; j<predictionResult[i].js.length; j++)
                {
                        print("\t"+"J("+predictionResult[i].atom+predictionResult[i].js[j].H1+", "+predictionResult[i].js[j].atom+predictionResult[i].js[j].H2+"): "+predictionResult[i].js[j].value);
                }
        }
}
